package com.sit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sit.entity.AddAthlete;
import com.sit.entity.Athlete;
import com.sit.entity.Ca;
import com.sit.mapper.AthleteMapper;
import com.sit.mapper.CaMapper;
import com.sit.mapper.CompetitionAMapper;
import com.sit.service.CaService;
import com.sit.vo.CaVO;
import com.sit.vo.DataVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CaServiceImpl implements CaService {

    @Autowired
    private CaMapper caMapper;

    @Autowired
    private AthleteMapper athleteMapper;

    @Autowired
    private CompetitionAMapper competitionAMapper;
    @Override
    public DataVO<CaVO> findData(Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        //分页查询
        //new一个page对象，用于封装page信息
        IPage<Ca> caIPage = new Page<>(page,limit);
        //通过查询再返回一个page对象
        IPage<Ca> result = caMapper.selectPage(caIPage,null);

        dataVO.setCount(result.getTotal());

        //List<Ca> caList = caMapper.selectList(null);
        //分页page对象不仅包含所有数据，还包含分页信息，所以直接通过result就能拿到所有记录
        List<Ca> caList = result.getRecords();
        List<CaVO> caVOList = new ArrayList<>();
        for (Ca ca : caList) {
            CaVO caVO = new CaVO();
            BeanUtils.copyProperties(ca,caVO);
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("ano",ca.getAno());
            caVO.setAn(athleteMapper.selectOne(wrapper).getAn());

            wrapper = new QueryWrapper();
            wrapper.eq("ano",ca.getAno());
            caVO.setSex(athleteMapper.selectOne(wrapper).getSex());

            wrapper = new QueryWrapper();
            wrapper.eq("ano",ca.getAno());
            caVO.setClassnumber(athleteMapper.selectOne(wrapper).getClassnumber());

            wrapper = new QueryWrapper();
            wrapper.eq("cno_a",ca.getCnoA());
            caVO.setCnA(competitionAMapper.selectOne(wrapper).getCnA());

            wrapper = new QueryWrapper();
            wrapper.eq("cno_a",ca.getCnoA());
            caVO.setPlaceA(competitionAMapper.selectOne(wrapper).getPlaceA());

            wrapper = new QueryWrapper();
            wrapper.eq("cno_a",ca.getCnoA());
            caVO.setDateA(competitionAMapper.selectOne(wrapper).getDateA());
            caVOList.add(caVO);
        }
        dataVO.setData(caVOList);
        return dataVO;
    }

    @Override
    public boolean deleteAthlete(String ano, String cnoA) {
        Map<String, Object> map = new HashMap<>();
        map.put("ano", ano);
        map.put("cno_a", cnoA);
        int flag = caMapper.deleteByMap(map);
        if (flag>0){
            return true;
        }else
            return false;
    }

    @Override
    public boolean updateAthlete(AddAthlete addAthlete,String cnA_new) {


        Athlete athlete = new Athlete();
        athlete.setAn(addAthlete.getAn());
        athlete.setSex(addAthlete.getSex());
        athlete.setClassnumber(addAthlete.getClassnumber());
        athlete.setCollege("计算机");
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("ano",addAthlete.getAno());
        int flag =athleteMapper.update(athlete,wrapper);

        Ca ca = new Ca();
        ca.setAno(addAthlete.getAno());
        ca.setCnoA(addAthlete.getCnA());
        Map<String, Object> map = new HashMap<>();
        map.put("ano",ca.getAno());
        map.put("cno_a", ca.getCnoA());
        int flag1 = caMapper.deleteByMap(map);

        ca.setCnoA(cnA_new);
        int flag2 =caMapper.insert(ca);
        if(flag >0 &&flag1 >0 && flag2 > 0){
            return  true;
        }else {return false;}
    }

    @Override
    public DataVO<CaVO> search(String ano, String cnA, Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        //分页查询
        //new一个page对象，用于封装page信息
        IPage<Ca> caIPage = new Page<>(page,limit);
        //通过查询再返回一个page对象
        QueryWrapper wrapper = new QueryWrapper();

        QueryWrapper wrapper2 = new QueryWrapper();
        wrapper2.eq("cn_a",cnA);
        String cnoA = competitionAMapper.selectOne(wrapper2).getCnoA();

        Map<String, Object> map = new HashMap<>();
        map.put("ano", ano);
        map.put("cno_a", cnoA);
        wrapper.allEq(map);
        IPage<Ca> result = caMapper.selectPage(caIPage,wrapper);

        dataVO.setCount(result.getTotal());

        //List<Ca> caList = caMapper.selectList(null);
        //分页page对象不仅包含所有数据，还包含分页信息，所以直接通过result就能拿到所有记录
        List<Ca> caList = result.getRecords();
        List<CaVO> caVOList = new ArrayList<>();
        for (Ca ca : caList) {
            CaVO caVO = new CaVO();
            BeanUtils.copyProperties(ca,caVO);
            QueryWrapper wrapper3 = new QueryWrapper();
            wrapper3.eq("ano",ca.getAno());
            caVO.setAn(athleteMapper.selectOne(wrapper3).getAn());

            wrapper3 = new QueryWrapper();
            wrapper3.eq("ano",ca.getAno());
            caVO.setSex(athleteMapper.selectOne(wrapper3).getSex());

            wrapper3 = new QueryWrapper();
            wrapper3.eq("ano",ca.getAno());
            caVO.setClassnumber(athleteMapper.selectOne(wrapper3).getClassnumber());

            wrapper3 = new QueryWrapper();
            wrapper3.eq("cno_a",ca.getCnoA());
            caVO.setCnA(competitionAMapper.selectOne(wrapper3).getCnA());

            wrapper3 = new QueryWrapper();
            wrapper3.eq("cno_a",ca.getCnoA());
            caVO.setPlaceA(competitionAMapper.selectOne(wrapper3).getPlaceA());

            wrapper3 = new QueryWrapper();
            wrapper3.eq("cno_a",ca.getCnoA());
            caVO.setDateA(competitionAMapper.selectOne(wrapper3).getDateA());
            caVOList.add(caVO);
        }
        dataVO.setData(caVOList);
        return dataVO;
    }

    @Override
    public boolean editScore(String ano, String cnoA, Integer rank, String score) {
        Ca ca = new Ca();
        ca.setAno(ano);
        ca.setCnoA(cnoA);
        Map<String, Object> map = new HashMap<>();
        map.put("ano",ca.getAno());
        map.put("cno_a", ca.getCnoA());
        int flag1 = caMapper.deleteByMap(map);

        ca.setRank(rank);
        ca.setScore(score);
        int flag2 =caMapper.insert(ca);
        if(flag1 >0 && flag2 > 0){
            return  true;
        }else {return false;}
    }


}
